<template>
  <div class="article-list">
    <!-- 下拉刷新 -->
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" :success-text="successText">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      >
        <van-cell v-for="article in list" :key="article.art_id">
          <div slot="title">
            <ArticleItem :article="article"></ArticleItem>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article.js'
import ArticleItem from '@/components/article-item'
export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [], //  存储数据
      loading: false, //  控制加载的状态
      finished: false, //  控制加载完成状态
      pre_timestamp: null, //  保存历史时间戳
      error: false,
      isLoading: false, //  控制下拉刷新加载中状态
      successText: '刷新成功'
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      //  loading 自动变为 true

      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1);
      //   }

      //   // 加载状态结束
      //   //  不设置 false 一直加载 不刷新
      //   this.loading = false;

      //   // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true;
      //   }
      // }, 1000);

      //  发送请求获取数据
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.pre_timestamp || Date.now(), //  时间戳
          with_top: 1
        })
        //  用于测试
        // if(Math.random()> 1) {
        //   throw new Error('错误')
        // }
        console.log(data)
        //  将数据追加到data 数组中保存
        const { results } = data.data
        // this.list.push(...results)
        // console.log(this.list);
        this.list = [...this.list, ...results]
        //  将loading设置false
        this.loading = false
        //  将时间戳更新实现翻页请求历史数据
        this.pre_timestamp = data.data.pre_timestamp
        //  判断数据是否加载完成
        if (data.data.pre_timestamp === null) {
          //  数据已加载完成
          this.finished = true
        }
      } catch (error) {
        this.loading = false
        this.error = true
        this.$toast('获取当前列表文章失败!')
      }
    },
    async onRefresh () {
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: Date.now(), //  时间戳
          with_top: 1
        })
        console.log(data)
        //  将数据追加到data 数组中保存
        const { results } = data.data
        this.list = [...results, ...this.list]
        this.isLoading = false
        this.successText = `刷新了 ${results.length} 条数据`
        // this.$toast('刷新成功!')
      } catch (error) {
        this.isLoading = false
        this.$toast('刷新失败!')
      }
    }
  }
}
</script>
<style lang='less' scoped>
  .article-list {
    height: 79vh;
    overflow-y: auto;
  }
</style>
